<template>
  <div v-loading="loading" class="activity-detail-page">
    <div v-if="activityInfo" class="detail-container">
      <div class="back-button" @click="router.back()">
        <el-icon style="color: #fff;"><ele-Back /></el-icon>
      </div>
      <div class="acitivity-banner">
        <el-carousel height="240">
          <el-carousel-item v-for="item in banners" :key="item.name">
            <img class="banner-img" :src="item.url" />
          </el-carousel-item>
        </el-carousel>
      </div>
      <div class="container" style="background-color: #fff">
        <div class="activity-title-wrap">
          <div class="activity-title">{{ activityInfo.name }}</div>
          <div class="activity-summary">
            {{ activityInfo.summary }}
          </div>
        </div>
        <ActivityApplyStatus
          size="large"
          :show-button="false"
          :price="applyInfo.price"
          :apply-num="applyInfo.applyNum"
          :total-num="applyInfo.totalNum"
        />
        <div style="margin-top: 18px">
          <div class="activity-info-cell">
            <div class="activity-info-cell-label">活动时间</div>
            <div class="activity-info-cell-value">
              {{ dayjs(activityInfo.start_date).format('YYYY.MM.DD') }} -
              {{ dayjs(activityInfo.end_date).format('YYYY.MM.DD') }}
            </div>
          </div>
          <div class="activity-info-cell">
            <div class="activity-info-cell-label">活动地址</div>
            <div class="activity-info-cell-value">
              {{ detailAddress }}
            </div>
            <div class="activity-info-cell-action">
              <div class="action-button">
                <el-icon class="action-button-icon"><ele-Location /></el-icon>
                <text>导航</text>
              </div>
            </div>
          </div>
          <div class="activity-info-cell">
            <div class="activity-info-cell-label">联系电话</div>
            <div class="activity-info-cell-value">
              {{ activityInfo.contact_phone }}（{{ activityInfo.contact_name }}）
            </div>
            <div class="activity-info-cell-action">
              <div class="action-button">
                <el-icon class="action-button-icon"><ele-Phone /></el-icon>
                <text>电话</text>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="container activity-content-wrap">
        <div class="content-tab-head">
          <div class="content-tab-head-label selected">活动详情</div>
          <div class="content-tab-head-label">活动照片</div>
        </div>
        <div class="content-wrap">
          <ActivityContent :content="activityInfo.content" />
        </div>
      </div>
      <SelectItem />
    </div>
  </div>
</template>

<script lang="ts" setup>
import ActivityContent from './components/activity-content.vue';
import ActivityApplyStatus from './components/activity-apply-status.vue';
import { readOneActivity } from '/@/api/activity';
import { useRoute, useRouter } from 'vue-router';
import { onMounted, computed, ref } from 'vue';
import dayjs from 'dayjs';

const route = useRoute();
const router = useRouter();
const activityInfo = ref();
const applyInfo = ref({
  price: 0,
  applyNum: 0,
  totalNum: 0
});
const loading = ref(false);

const banners = computed(() => {
  return activityInfo.value?.banners || [];
});

const detailAddress = computed(() => {
  if (!activityInfo.value) {
    return '';
  }

  const { area_names, address } = activityInfo.value;
  return area_names.join('') + address;
});

onMounted(async () => {
  loading.value = true;
  const { data } = await readOneActivity(route?.query?.id as string);
  loading.value = false;
  activityInfo.value = data;
  const { activity_items } = data;

  applyInfo.value = {
    price: Math.min(...activity_items.map((o: any) => o.price)),
    applyNum: activity_items.reduce((prev, o) => prev + o.apply_num, 0),
    totalNum: activity_items.reduce((prev, o) => prev + o.total, 0)
  };
});
</script>

<style lang="scss" scoped>
.activity-detail-page {
  position: relative;
  margin: 20px auto;
  width: 412px;
  height: 833px;
  padding: 56px 15px 50px;
  background: url('../../assets/iphone13.png') no-repeat;
  background-size: 100%;
  background-color: #f8f8f8;
}

.detail-container {
  background: #f5f5f5;
  position: relative;
  height: 100%;
  overflow-y: auto;
}

.container {
  position: relative;
  padding: 0 15px;
}

.back-button {
  position: absolute;
  top: 24px;
  left: 12px;
  width: 30px;
  height: 30px;
  z-index: 101;
  background: rgba(#000, 0.3);
  border-radius: 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  .icon-back {
    width: 16px;
    height: 16px;
  }
}

.activity-info-cell {
  height: 54px;
  display: flex;
  align-items: center;
  overflow: hidden;
  font-size: 13px;
  border-top: 1px solid rgba(#000, 0.05);

  &-label {
    margin-right: 20px;
    color: rgba(0, 0, 0, 0.5);
  }

  &-value {
    flex: 1;
    color: rgba(0, 0, 0, 0.9);
    padding-right: 15rpx;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  &-action {
    .action-button {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 56px;
      height: 24px;
      border-radius: 4px;
      background: linear-gradient(113deg, rgba(#f9865d, 0.1) 0%, rgba(#f75d24, 0.1) 100%);
      font-size: 12px;
      color: #f75d24;
    }

    .action-button-icon {
      margin-right: 3px;
      width: 14px;
      height: 14px;
    }
  }
}

.activity-title {
  padding-top: 12px;
  font-size: 20px;
  font-weight: 600;
  color: #000;
  line-height: 28px;
}

.activity-summary {
  margin-bottom: 12px;
  margin-top: 6px;
  font-size: 13px;
  line-height: 18px;
  color: rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

.activity-content-wrap {
  margin-top: 16px;
  padding-bottom: 74px;
  background-color: #fff;
}

.banner-img {
  object-fit: cover;
  width: 100%;
  height: 100%;
}

.content-tab-head {
  display: flex;
  padding-top: 20px;
  padding-bottom: 6px;

  &-label {
    position: relative;
    margin-right: 40px;
    font-size: 16px;
    font-weight: 600;
    line-height: 1;
    color: rgba(0, 0, 0, 0.5);
    padding-bottom: 8px;

    &.selected {
      color: rgba(0, 0, 0, 0.9);

      &::after {
        position: absolute;
        bottom: 0;
        left: 50%;
        content: '';
        transform: translateX(-50%);
        width: 56px;
        border-top: 2px solid #f36d3c;
      }
    }
  }
}
</style>
